package com.gitee.blogcase.dealyqueuecase.rabbitmq.delayed.consumer;

import com.gitee.blogcase.dealyqueuecase.rabbitmq.deadletter.config.DeadLetterQueueConfig;
import com.gitee.blogcase.dealyqueuecase.rabbitmq.delayed.config.DelayedQueueConfig;
import com.gitee.blogcase.dealyqueuecase.utils.DateUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 延迟消息插件订单超时案例
 * @author wwuwentao
 */
@Slf4j
@Component
public class DelayedConsumerAnnotatedEdition {
    /**
     * 延迟队列交换机类型必须为：x-delayed-message
     * x-delayed-type 必须设置否则将会报错
     */
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = DelayedQueueConfig.DELAYED_QUEUE),
                    exchange = @Exchange(value = DelayedQueueConfig.DELAYED_EXCHANGE,type = "x-delayed-message",
                    arguments = {@Argument(name = "x-delayed-type", value = ExchangeTypes.DIRECT)}),
                    key = {DelayedQueueConfig.DELAYED_ROUTING_KEY}
            )
    })
    @RabbitHandler
    public void delayedConsumer(String context, Message message, Channel channel) throws Exception {
        log.info("当前时间:{} 订单取消订单号:{}", DateUtil.getCuurentDateStr(),context);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);//仅确认本条消息
    }
}
